﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CodeGenerator.Core
{
    /// <summary>
    /// 判断字段类型及UI类型
    /// </summary>
    public static class DataTypeHelper
    {
        public static bool IsNumeric(string dataType)
        {
            bool result = false;
            if (!string.IsNullOrEmpty(dataType))
            {
                string text = dataType.ToLower();
                if (text.Contains("int") || text.Contains("decimal") || text.Contains("double") || text.Contains("single") || text.Contains("byte") || text.Contains("short") || text.Contains("float"))
                {
                    result = true;
                }
            }
            return result;
        }

        public static bool IsDateTime(string dataType)
        {
            bool result = false;
            if (!string.IsNullOrEmpty(dataType) && dataType.ToLower().Contains("datetime"))
            {
                result = true;
            }
            return result;
        }

        public static bool IsBoolean(string dataType)
        {
            bool result = false;
            if (!string.IsNullOrEmpty(dataType) && dataType.ToLower().Contains("bool"))
            {
                result = true;
            }
            return result;
        }

        public static string GetUIControlType(ColumnInfo info)
        {
            ControlType controlType = ControlType.Text;
            if (info.IsDateTime)
            {
                controlType = ControlType.DateTime;
            }
            else if (info.IsNumeric)
            {
                controlType = ControlType.Numeric;
            }
            else if (info.IsBoolean)
            {
                controlType = ControlType.CheckBox;
            }
            else if (info.DataType.ToLower().Contains("text"))
            {
                controlType = ControlType.TextArea;
            }
            else if (info.DataType.ToLower().Contains("varchar") && info.MaxLength >= 2147483647)
            {
                controlType = ControlType.TextArea;
            }
            return controlType.ToString();
        }
    }
}
